package com.tulingxueyuan.mall.controller;

import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.AddCarDTO;
import com.tulingxueyuan.mall.dto.CarItemStockDTO;
import com.tulingxueyuan.mall.modules.oms.model.OmsCartItem;
import com.tulingxueyuan.mall.modules.oms.service.OmsCartItemService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(tags = "CarController", description = "购物车服务接口")
@RequestMapping("/car")
@ResponseBody
public class CarController {
    @Autowired
    private OmsCartItemService cartItemService;
    /**
     * .post("/car/add", {
     *           productId: this.id,
     *           productSkuId: this.skuId,
     *           quantity: 1,
     *         })
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody AddCarDTO addCarDTO){
      Boolean result=  cartItemService.add(addCarDTO);
      if (result){
          return CommonResult.success(result);
      }
      return CommonResult.failed();
    }
    /**
     * 获取购物车商品数量
     * this.axios.get('/car/products/sum').then((res=0)=>{
     *         // 存全局变量
     *         this.$store.dispatch('saveCartCount',res);
     */
    @GetMapping("/products/sum")
    public CommonResult getCarProductsSum(){
      Integer sum= cartItemService.getCarProductsSum();
     return CommonResult.success(sum);
    }
    /**
     * 获取购物车数据初始化
     * /car/list
     */
    @GetMapping("/list")
    public CommonResult getList(){
        List<CarItemStockDTO> list =cartItemService.getList();
        return CommonResult.success(list);
    }
    /**
     * 更新商品数量
     * this.axios.post('/car/update/quantity'
     */
    @PostMapping("/update/quantity")
    public CommonResult updateQuantity(@RequestParam Long id,
                                       @RequestParam Integer quantity  ){
        Boolean result= cartItemService.updateQuantity(id,quantity);
        if (result){
            return CommonResult.success(result);
        }
        return CommonResult.failed();

    }
    /**  删除购物车商品
     *   this.axios.post('/car/delete',Qs.stringify({
     *             ids:item.id
     */
    @PostMapping("/delete")
    public CommonResult deleteQuantity(@RequestParam Long ids){
        Boolean result= cartItemService.delete(ids);
        if (result){
            return CommonResult.success(result);
        }
        return CommonResult.failed();

    }


}
